package mobi.weq.util.edinburgh;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import mobi.weq.dao.SensorObservationDao;
import mobi.weq.model.SensorObservation;
import mobi.weq.util.Util;

public class EdinburghDataGeneratorInSplitTable {

	public static void generateDataForCurrentHour() throws SQLException {
		SensorObservationDao dao = new SensorObservationDao();
		dao.addObservationsInTable(EdinburghGeneratorUtil.generateEdinburghSensorHourlyObservationsThroughoutCityEveryGridForCurrentHour(),
				getTableName(new Date()));
	}

	// double k = 407, j = 413;
	// double j = 64, k = 43;

	// lat diff:0.07344955795602459
	// lng diff :0.54931640625
	public static void generateData(int days) throws SQLException {
		// double increment = 1.0 / EdinburghMap.GRID_DENSITY;
		// System.out.println(EdinburghMap.LAT2 + increment / 10);
		long t = new Date().getTime();
		Date d = new Date();
		d.setMinutes(0);
		d.setSeconds(0);
		d.setHours(23);
		d = Util.decreaseDateByDay(d, -90);

		SensorObservationDao dao = new SensorObservationDao();
		// dao.addObservationsInTable(EdinburghGeneratorUtil.generateEdinburghSensorHourlyObservationsThroughoutCityEveryGrid(d),
		// getTableName(d));
		// d = Util.decreaseDateByDay(d, 1);
		//
		// d.setHours(23);

		for (int i = 0; i < days; i++) {
			ArrayList<SensorObservation> o = EdinburghGeneratorUtil.generateEdinburghSensorHourlyObservationsThroughoutCityEveryGrid(d);
			System.out.println(d + "#" + o.size() + "::" + getTableName(d));
			dao.addObservationsInTable(o, getTableName(d));
			d = Util.decreaseDateByDay(d, 1);
			System.out.println("Time in Seconds: " + (new Date().getTime() - t) / 1000);
		}
	}

	public static String getTableName(Date d) {
		return "so_" + d.getDate() + "_" + (d.getMonth() + 1) + "_" + (d.getYear() + 1900);
	}

	public static void main(String[] args) {
		 try {
		 generateData(200);
		 // generateDataForCurrentHour();
		 } catch (SQLException e) {
		 // TODO Auto-generated catch block
		 e.printStackTrace();
		 }

		// Date d = new Date();
		// d = Util.decreaseDateByDay(d, -90);
		// for (int i = 0; i < 200; i++) {
		// System.out.println("create table so_" + d.getDate() + "_" +
		// (d.getMonth() + 1) + "_" + (d.getYear() + 1900) +
		// " as (select * from so_template);");
		// d = Util.decreaseDateByDay(d, 1);
		// }

		// d = new Date();
		// for (int i = 0; i < 90; i++) {
		// System.out.println("drop table so_" + d.getDate() + "_" +
		// (d.getMonth() + 1) + "_" + (d.getYear() + 1900) + ";");
		// d = Util.decreaseDateByDay(d, 1);
		// }

	}
}
